Skip to content

Instantly share code, notes, and snippets.

@funseiki
Created March 8, 2015 23:41
Show Gist options
  • Save funseiki/80a40bec9f0dfdf73979 to your computer and use it in GitHub Desktop.
Save funseiki/80a40bec9f0dfdf73979 to your computer and use it in GitHub Desktop.
Parse to Twitter Digits validation
// Uses retrofit to generate an implementation
public interface ICloud {
static String serviceProviderHeader = "X-Auth-Service-Provider";
static String credentialsAuthorizationHeader = "X-Verify-Credentials-Authorization";
@GET("/verify_credentials?provider=digits")
void verifyCredentials(
@Header(serviceProviderHeader) String serviceProvider,
@Header(credentialsAuthorizationHeader) String authorization,
@Query("id") long id,
Callback<String> callback);
}
public class SessionManager {
// ... //
// region Session related
public void verifySession(DigitsSession session, Callback callback) {
TwitterAuthConfig authConfig = TwitterCore.getInstance().getAuthConfig();
// Cast from AuthToken to TwitterAuthToken
TwitterAuthToken authToken = (TwitterAuthToken)session.getAuthToken();
OAuthSigning oAuthSigning = new OAuthSigning(authConfig, authToken);
// First value should be the location we're querying to twitter.
// The second is the actual validation information
Map<String, String> authHeaders = oAuthSigning.getOAuthEchoHeadersForVerifyCredentials();
try {
Cloud.verifyCredentials(
authHeaders.get(ICloud.serviceProviderHeader),
authHeaders.get(ICloud.credentialsAuthorizationHeader),
session.getId(),
callback);
}
catch (Exception e) {
Log.e(LOG_TAG, e.getMessage().toString());
}
}
}
// The webhooks module
var express = require('express')
, _ = require('underscore');
/**
* Handles authorizing with Twitter
*/
function DigitsAuth(req, res) {
// These are the input fields coming in
var headers = [
// The url we'll be making the twitter request to
'X-Auth-Service-Provider',
// These are the credentials twitter will be verifying
'X-Verify-Credentials-Authorization'
];
// Check for any missing inputs
var missingValues = _.reject(headers, function(val) {
return req.get(val);
});
// Return an error if we don't have proper inputs
if(missingValues.length > 0) {
res.status(400).json({
error: 'Auth information missing',
missingInputs: missingValues
});
console.log('Missing values', missingValues);
return;
}
// Send the request to twitter
Parse.Cloud.httpRequest({
method: 'GET',
url: req.get(headers[0]),
headers: {'Authorization': req.get(headers[1])},
success: function(httpResponse) {
var obj = JSON.parse(httpResponse.text);
// TODO: Check for errors. Add to sessions table. Send JSON response
res.status(httpResponse.status).send("success");
},
error: function(httpResponse) {
res.status(400).json({
error: 'Unable to make a twitter request'
});
console.log('Twitter error', httpResponse.text);
}
});
}
/** Setup Express **/
var app = express();
app.use(express.bodyParser()); // Middleware for reading request body
app.get('/verify_credentials', DigitsAuth);
@benitech
Copy link

benitech commented Apr 2, 2016

Do you have a working example that uses Digits plus one other Social provider, let's say Google or FB? It would be really helpful as a demo for the new opensource parse server.

Lot of devs are struggling with the proper oauth implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment