I'm working with the instagram API and I don't understand what I should put in for the redirect api. At the moment I simply put in https://127.0.0.1
But I dont really understand. Also, once I get the allow/cancel screen and I press allow it gives me an error saying that I cant go to that address but I can also see the authorization code appended on to the address. So how can i redirect back from my redirect uri? How can I tell android that after user clicks allow to come back into the app use the code for further authentication?
Im sure you will say something like make my own custom scheme/ intent filters etc but please be a little more supportive im new and I dont understand and I did do research on them.
My on resume method is below
#Override
protected void onResume() {
super.onResume();
// the intent filter defined in AndroidManifest will handle the return from ACTION_VIEW intent
Uri uri = getIntent().getData();
if (uri != null && uri.toString().startsWith(redirectUri)) {
// use the parameter your API exposes for the code (mostly it's "code")
String code = uri.getQueryParameter("code");
if (code != null) {
// get access token
LoginService loginService =
ServiceGenerator.createService(LoginService.class, clientId, clientSecret);
AccessToken accessToken = loginService.getAccessToken(code, "authorization_code");
} else if (uri.getQueryParameter("error") != null) {
// show an error message here
}
}
}
This is my manifest snippet dealing with intent filters:
<activity
android:name=".LoginActivity"
android:label="#string/title_activity_login" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="redirecturi"
android:scheme="your" />
</intent-filter>
</activity>
You have to setup an event listener on the browser view and check for code in URL param if the URL is equal to the redirect_uri, and then make POST request to the auth URL using the code and client_secret as documented in Instagram authentication
something like this:
webView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
String code = url.getQueryParameter("code");
// ...
}
});
Related
I am implementing a shopping app.In that when a product is shared it should send a dynamic link so that when a person clicks it , opens in app only.Finally i achieved this feature.But when i am opening app from a shared dynamic link i am getting home page rather than product page.I need help regarding getting specific product page rather opening home page of shopping.
Here i am including code. AndroidManifest.xml
<activity android:name=".Postdetails">
<meta-data android:name="android.support.PARENT_ACTIVITY" android:value="MainActivity" />
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data android:host="postdetailsnew.com" android:scheme="https"
android:pathPattern=".*"/>
</intent-filter>
</activity>
PostDetails.java Here PostDetails page is the single product design page or activity .In this i wrote following code.
sharebutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
s="https://app_code.app.goo.gl/?link="+ProductLinkInBrowser+"&apn=com.example.maneesh.shop";
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, s);
sendIntent.setType("text/plain");
startActivity(sendIntent);
}
}
FirebaseDynamicLinks.getInstance().getDynamicLink(getIntent())
.addOnSuccessListener(this, new OnSuccessListener<PendingDynamicLinkData>() {
#Override
public void onSuccess(PendingDynamicLinkData pendingDynamicLinkData) {
// Get deep link from result (may be null if no link is found)
Uri deepLink = null;
if (pendingDynamicLinkData != null) {
deepLink = pendingDynamicLinkData.getLink();
FirebaseAppInvite invite=FirebaseAppInvite.getInvitation(pendingDynamicLinkData);
if(invite!=null){
String invitationId=invite.getInvitationId();
}
}
}
})
.addOnFailureListener(this, new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Log.w("Heyy", "getDynamicLink:onFailure", e);
}
});
So here finally app main page is opened with dynamic link but not the specific product page which is shared.Please guide me to achieve this.
You need to parse the URL in the dynamic link, and fire an intent to send the user to the right part of your app. Generally you'll have the Dynamic Links listener set up in your main activity, and will route from there to any specific needs.
The deepLink variable in your code will be the URL your passed in the dynamic link, like http://yourshop.com/product/12345. You could call deepLink.getPath() which would return you product/12345, and then fire and intent for that, e.g:
String path = deepLink.getPath();
String[] parts = path.split("/")
if (parts[0] == "product") {
Intent intent = new Intent(this, PostDetails.class);
intent.putExtra("productid", parts[1]);
startActivity(intent);
}
I've read the post that everyone links to about link handling clicks with TextViews.
I have a textview with a Movement method set and it seems to be working correctly. When I click on the link it opens a browser and displays the correct information.
My FormatUtils.generateHtml class takes markdown from submission.getContent() and creates usable links as well as formates the text to bold and italics and such.
For some reason I am stumped on how to get that info to load into my own webview.
My manifest for the activity
<activity android:name="com.matteo.example.view.activities.SubmissionDetailActivity">
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.VIEW" />
<data android:scheme="com.matteo.example" />
</intent-filter>
</activity>
Linked Textview
CharSequence string = FormatUtils.generateHtml(submission.getContent());
formattedContent.setText(string);
formattedContent.setMovementMethod(LinkMovementMethod.getInstance());
in my Activity with the webview. These are my attepts to get the data in the intent. Am I even close here?
mywebview.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return true;
}
});
#Override
public void startActivity(Intent intent) {
if (TextUtils.equals(intent.getAction(), Intent.ACTION_VIEW)) {
Bundle extras = getIntent().getExtras();
intent = getIntent();
String extra = intent.getStringExtra();
Uri data = getIntent().getData();
mywebview.loadUrl(intent.toString());
Toast.makeText(this, "THIS IS INTENT DATA~~ " + intent.getDataString() , Toast.LENGTH_LONG).show();
So far it seems eveything is null.
How do I get the data from the Intent so I can handle it with my webview?
I have a specific URL that I want redirected to a specific activity in my app from a webview with an intent filter. How to implement my very own URI scheme on Android described how to do this for a browser page, but this same intent filter doesn't work when that URL is accessed through the webview. Is there anything else that needs to be added to this intent filter to catch these webview links?
<intent-filter>
<action android:name="android.intent.action.VIEW"></action>
<category android:name="android.intent.category.DEFAULT"></category>
<category android:name="android.intent.category.BROWSABLE"></category>
<data android:host="myurl.com/stuff" android:scheme="http"></data>
</intent-filter>`
I have not got intent filters and webviews to work together just with declaring the intent on the manifest and I think they are not supposed to. (I wonder why...) I think the way to do it is catching urls when you are trying to open them in the webview and creating an intent then.
Then, for an activity register as follows in the manifest:
<activity android:name=".PretendChat">
<intent-filter>
<action android:name="android.intent.action.VIEW"></action>
<category android:name="android.intent.category.DEFAULT"></category>
<category android:name="android.intent.category.BROWSABLE"></category>
<data android:host="chat" ></data>
<data android:scheme="testing"></data>
<data android:pathPattern=".*"></data>
</intent-filter>
</activity>
You would expect the activity PretendChat opens when you click on a link like the following: "testing://chat" inside the webview. In order for that to happen you
would need the following code on the webview client you are using on the webview. Assume that the activity that starts the webview is called WebviewActivity.
private class TestWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
try {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
WebviewActivity.this.startActivity(intent);
} catch(ActivityNotFoundException e) {
Log.e(LOGTAG,"Could not load url"+url);
}
return super.shouldOverrideUrlLoading(view, url);
}
}
The only way I got it to work was to use a dummy file link and loading the URL with loadDataWithBaseURL and using relative links in the data. The latest WebView only seems to accept valid links, and I couldn't get it to work with custom intents for Activities.
If I tried a different scheme, such as "my-app" instead of "file", the url in shouldOverrideUrlLoading would come up as "about:blank".
I also wanted to pass parameters to the activity to be opened in Bundle.
webView.loadDataWithBaseURL("file:///android_asset", w.Definition, "text/html", "utf-8", null);
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("file")) {
Intent intent = new Intent(DictWordActivity.this, DictWordActivity.class);
intent.putExtra("word", Uri.parse(url).getLastPathSegment());
startActivity(intent);
return true;
} else
return false;
}
});
I created my relative links like this:
"" + word + "");
If anyone has a better solution I would like to know.
i have loads of examples for O_Auth but no one is working properly and i guess the only thing is callback URL in every example, I am getting 401 error like consumer key or signature didn't match. I already seen so many examples on 401 the only thing that goes into my favor is my callback url.
android app cannot connect to twitter.
Getting 401 when requesting access token with signpost within android with-signpost-within-android
Android Dev - Callback URL not working... (0_o)
I already seen all these examples and many more.
But still i am not getting my point, If at the time of application form i use http://www.meomyo.com at callback url, Then what should i use it in my coding
like android:scheme="?" android:host="?"
currrently i am using other examples callbacks
//private static final Uri CALLBACK_URI = Uri.parse("bloa-app://twitt");
private static final Uri CALLBACK_URI = Uri.parse("twitterapp://connect");
i have consumer key as well secret key, but in case of callback url i am stuck on it.
If anyone want i can provide my consumer as well secret key.
public class OAuth extends Activity {
private static final String APP = "OAUTH";
private Twitter twitter;
private OAuthProvider provider;
private CommonsHttpOAuthConsumer consumer;
private String CONSUMER_KEY = "Xh3o8Gh1JQnklkUnTvvA";
private String CONSUMER_SECRET = "SCLy6yoUSth53goAsYYkoqR4ZuBoaInyJXsm5PQR11I";
private String CALLBACK_URL = "merabharatmahan://piyush";
private TextView tweetTextView;
private Button buttonLogin;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tweetTextView = (TextView)findViewById(R.id.tweet);
buttonLogin = (Button)findViewById(R.id.ButtonLogin);
buttonLogin.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
askOAuth();
}
});
}
/**
* Open the browser and asks the user to authorize the app.
* Afterwards, we redirect the user back here!
*/
private void askOAuth() {
try {
consumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
provider = new DefaultOAuthProvider("http://twitter.com/oauth/request_token",
"http://twitter.com/oauth/access_token",
"http://twitter.com/oauth/authorize");
String authUrl = provider.retrieveRequestToken(consumer, CALLBACK_URL);
Toast.makeText(this, "Please authorize this app!", Toast.LENGTH_LONG).show();
this.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl)));
} catch (Exception e) {
Log.e(APP, e.getMessage());
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
/**
* As soon as the user successfully authorized the app, we are notified
* here. Now we need to get the verifier from the callback URL, retrieve
* token and token_secret and feed them to twitter4j (as well as
* consumer key and secret).
*/
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Uri uri = intent.getData();
if (uri != null && uri.toString().startsWith(CALLBACK_URL)) {
String verifier = uri.getQueryParameter(oauth.signpost.OAuth.OAUTH_VERIFIER);
try {
// this will populate token and token_secret in consumer
provider.retrieveAccessToken(consumer, verifier);
// TODO: you might want to store token and token_secret in you app settings!!!!!!!!
AccessToken a = new AccessToken(consumer.getToken(), consumer.getTokenSecret());
// initialize Twitter4J
twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
twitter.setOAuthAccessToken(a);
// create a tweet
Date d = new Date(System.currentTimeMillis());
String tweet = "#OAuth working! " + d.toLocaleString();
// send the tweet
twitter.updateStatus(tweet);
// feedback for the user...
tweetTextView.setText(tweet);
Toast.makeText(this, tweet, Toast.LENGTH_LONG).show();
buttonLogin.setVisibility(Button.GONE);
} catch (Exception e) {
Log.e(APP, e.getMessage());
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
}
manifest code is
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".OAuth"
android:label="#string/app_name"
android:launchMode="singleInstance">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="merabharatmahan" android:host="piyush" />
</intent-filter>
</activity>
</application
now i am getting communication with the service provider is failed: Received Authentication challenge is null. It is the simplest example that i put here.
twitterapp://connect is your call back url.
In Android manifest.xml define your activity like this:
<activity android:name=".auth.SocialNetworkActivity"
android:configChanges="orientation"
android:label="Connect SNS" android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="twitterapp" android:host="connect" />
</intent-filter>
</activity>
Now, it will open twitter web page, and on successful authentication, your activities onNewIntent() callback method will be called.
In above example, use your own activity name.
Apart from that, twitter integration in my application is done using twitter4j.
You must put whatever you think will be unique:
android:scheme="name-of-your-dog" android:host="something-else"
Oh, wait... maybe there's someone else owning a dog called like yours... so, use the package name of your app:
android:scheme="com.your.package" android:host="something-else"
And, if it's not working for you... I don't think it's related to the callback URL. How are you declaring your activity in the manifest? You added the android:launchMode="singleTask" parameter, right?
And, as the zombie guy pointed out, you must handle the callback in the onNewIntent method of your auth activity. Something like this:
final Uri uri = intent.getData();
if (uri != null && uri.getScheme().equals("name-of-your-dog")) {
//etc...
}
My Android application uses Java OAuth library, found here for authorization on Twitter. I am able to get a request token, authorize the token and get an acknowlegement but when the browser tries the call back url to reconnect with my application, it does not use the URL I provide in code, but uses the one I supplied while registering with Twitter.
Note:
1. When registering my application with twitter, I provided a hypothetical call back url:http://abz.xyc.com and set the application type as browser.
2. I provided a callback url in my code "myapp" and have added an intent filter for my activity with Browsable category and data scheme as "myapp".
3. URL called when authorizing does contain te callback url, I specified in code.
Any idea what I am doing wrong here?
Relevant Code:
public class FirstActivity extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
OAuthAccessor client = defaultClient();
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(client.consumer.serviceProvider.userAuthorizationURL + "?oauth_token="
+ client.requestToken + "&oauth_callback=" + client.consumer.callbackURL));
startActivity(i);
}
OAuthServiceProvider defaultProvider()
{
return new OAuthServiceProvider(GeneralRuntimeConstants.request_token_URL,
GeneralRuntimeConstants.authorize_url, GeneralRuntimeConstants.access_token_url);
}
OAuthAccessor defaultClient()
{
String callbackUrl = "myapp:///";
OAuthServiceProvider provider = defaultProvider();
OAuthConsumer consumer = new OAuthConsumer(callbackUrl,
GeneralRuntimeConstants.consumer_key, GeneralRuntimeConstants.consumer_secret,
provider);
OAuthAccessor accessor = new OAuthAccessor(consumer);
OAuthClient client = new OAuthClient(new HttpClient4());
try
{
client.getRequestToken(accessor);
} catch (Exception e)
{
e.printStackTrace();
}
return accessor;
}
#Override
protected void onResume()
{
// TODO Auto-generated method stub
super.onResume();
Uri uri = this.getIntent().getData();
if (uri != null)
{
String access_token = uri.getQueryParameter("oauth_token");
}
}
}
// Manifest file
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".FirstActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="myapp"/>
</intent-filter>
</activity>
</application>
Twitter does not honor callbacks requested in OAuth requests (Twitter API Announce) and will only redirect to the callback URL specified in the Application Settings (note that "localhost" is not allowed).
I assume you checked Oauth-callback-on-android question.
Android guesswork--
After a bit of reading up, I see Android browser redirects MyApp:/// to your application and I'm guessing Twitter doesn't like this bespoke URI prefix. I'm no android developer but one suggestion I might make is to get "www.myapp.com" on the web and have a re-redirect there.
So have your OAuth return to http://www.myapp.com/redirect.aspx?oauth_token=abc and have that page redirect to myapp:///oauth_token=... (the desired result)
In my case, i have this working:
String authURL = m_provider.retrieveRequestToken (m_consumer, CALLBACK_URL);
And in the Manifest:
<activity android:configChanges = "keyboardHidden|orientation" android:name = "xxxx.android.xxxxx">
<intent-filter>
<action android:name = "android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="myapp" android:host="tweet" />
</intent-filter>
In this case the callback url will be: myapp://tweet
It looks to me like you're doing the correct thing and Twitter is screwing up by always accepting your registered callback URL. Is there any way to change the registered URL? Maybe you could re-register and try an Android callback next time, see what happens.
My problem was that I was trying to log in with the same account I made the Twitter app. After I logged in with my personal profile the call back works (so far).